if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function () {
    if (AGENT_CLIENT.WEB) {
        ready();
    }
})

function ready() {
    var thisPage = null, html = [], user = null, inviteUrl = "";
    var fromurl = getUrlParam('fromurl');
    var _currentloginuser = getLocalStorage(STORAGE_MANAGER.user);
    var invitePage = function () {
        this.init();
    };

    invitePage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user) {
                redirect("../account/login.html");
            }
            inviteUrl = "http://wap.soolife.cn/view/account/register.html?memberid=" + user.id;
            $("#inviteLink").val(inviteUrl);
            $("#copyurltip").text(AGENT_CLIENT.APP ? "(点击复制下面链接)" : "(选中下方链接,复制)");
            //二维码
            getBarImage($("#inviteBarcode"), inviteUrl);
        },
        bindEvent: function () {
            //点击后退
            $("#back").on("click", function () {
              backToPreviousPage(fromurl,false);
            });
            //查看邀请记录
            $("#inviteList").on("click", function () {
                redirect("invitelist.html");
            });

            //点击链接复制
            $("#inviteLink").on("click", function () {
                if (AGENT_CLIENT.APP) {
                    if (copyToClip($(this).val())) {
                        $.toast("链接已复制");
                    }
                    else {
                        $.toast("复制失败");
                    }
                } else {
                    $(this)[0].select();
                }
            });


            //保存我的二维码
            if (AGENT_CLIENT.APP) {
                $("#inviteBarcode").longPress(function () {
                    $.confirm("保存到相册?", function (e) {
                        if (e.index == 0) {
                            var imageUrl = $("#inviteBarcode").attr("src");
                            console.log(imageUrl);
                            saveGallery(imageUrl);
                        }
                    }, "提示", ["是", "否"]);
                });
            } else {
                $("#inviteBarcode").click(function () {
                    document.execCommand('SaveAs');
                });
            }

            // 红包分享
            $("#redpaper").on("click", function () {
                _sharePage.show();
            });
        }
    };

    thisPage = new invitePage();
    // 分享页面
    var _sharePage = new sharePage();
    _sharePage.shareInfo = {
        title: "马上领取10元现金红包！",
        content: "推荐好友各得现金红包，免费换零食家居护肤品！",
        href: "http://wap.soolife.cn/view/mine/redpackage.html?memberid=" + _currentloginuser.id,
        pic: "http://wap.soolife.cn/images/hongbao.png",
        picid: "redpaper"
    };
}
